<template>
  <div v-loading="loading" class="bg-white">
    <el-page-header :content="content" @back="$emit('back')"></el-page-header>
    <el-form ref="form" :model="form" :rules="rules" class="demo-ruleForm pa-10" label-width="110px">
      <p class="bold">基本信息</p>
      <el-row>
        <el-col :span="12">
          <el-form-item label="模板编码：" prop="templateCode">
            <el-input v-model="form.templateCode" :disabled="type == 'view'" size="small"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="模板名称：" prop="templateName">
            <el-input v-model="form.templateName" :disabled="type == 'view'" size="small"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="模板说明：" prop="note">
            <el-input v-model="form.note" :disabled="type == 'view'" size="small" type="textarea"></el-input>
          </el-form-item>
        </el-col>
        <p class="bold">渠道信息</p>
        <el-col :span="24">
          <el-form-item label="发送渠道：" prop="sendWay">
            <el-radio-group v-model="form.sendWay" :disabled="type == 'view'" size="small">
              <el-radio label="notice">站内通知</el-radio>
              <el-radio label="sms">短信</el-radio>
              <el-radio label="email">邮箱</el-radio>
              <el-radio label="feishu_robot">飞书</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <template v-if="form.sendWay === 'notice'">
          <el-col :span="24">
            <el-form-item label="发送标题：" prop="title">
              <el-input
                v-model="form.title"
                :disabled="type == 'view'"
                placeholder="示例：通知标题${title}"
                size="small"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="发送内容：" prop="content">
              <el-input
                v-model="form.content"
                :disabled="type == 'view'"
                placeholder="示例：您的验证码为${code}"
                size="small"
                type="textarea"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="处理链接：" prop="dealLink">
              <el-input
                v-model="form.dealLink"
                :disabled="type == 'view'"
                placeholder="示例：请点击${link}跳转"
                size="small"
              ></el-input>
            </el-form-item>
          </el-col>
        </template>
        <template v-if="form.sendWay === 'sms'">
          <el-col :span="24">
            <el-form-item label="短信内容：" prop="content">
              <el-input
                v-model="form.content"
                :disabled="type == 'view'"
                placeholder="示例：您的验证码为${code}"
                size="small"
              ></el-input>
            </el-form-item>
          </el-col>
        </template>
        <template v-if="form.sendWay === 'email'">
          <el-col :span="24">
            <el-form-item label="邮件标题：" prop="title">
              <el-input
                v-model="form.title"
                :disabled="type == 'view'"
                placeholder="示例：通知标题${title}"
                size="small"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="邮件内容：" prop="content">
              <el-input
                v-model="form.content"
                :disabled="type == 'view'"
                placeholder="示例：您的验证码为${code}"
                size="small"
                type="textarea"
              ></el-input>
            </el-form-item>
          </el-col>
        </template>
        <template v-if="form.sendWay === 'feishu_robot'">
          <el-col :span="24">
            <el-form-item label="发送标题：" prop="title">
              <el-input
                v-model="form.title"
                :disabled="type == 'view'"
                placeholder="示例：通知标题${title}"
                size="small"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="发送内容：" prop="content">
              <el-input
                v-model="form.content"
                :disabled="type == 'view'"
                placeholder="示例：您的验证码为${code}"
                size="small"
                type="textarea"
              ></el-input>
            </el-form-item>
          </el-col>
        </template>
      </el-row>
      <el-form-item label-width="0">
        <el-button v-if="type !== 'view'" size="small" type="primary" @click="submitForm('form')">保 存</el-button>
        <el-button icon="el-icon-back" size="small" @click="$emit('back')">{{
          type !== 'view' ? '取消' : '关闭'
        }}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { requesInfoTemplateDetail, requestAddInfoTemplate, requestEditInfoTemplate } from '@/api'

export default {
  name: 'Detail',
  props: {
    templateId: {
      type: [Number, String],
      default: '',
    },
    type: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      loading: false,
      form: {},
      content: '新增模板',
      rules: {
        templateCode: [{ required: true, message: '请输入', trigger: 'blur' }],
        templateName: [{ required: true, message: '请输入', trigger: 'blur' }],
        sendWay: [{ required: true, message: '请选择', trigger: 'change' }],
        title: [{ required: true, message: '请输入', trigger: 'blur' }],
        content: [{ required: true, message: '请输入', trigger: 'blur' }],
      },
    }
  },
  created() {
    this.content = this.type == 'add' ? '新增消息模板' : this.type == 'edit' ? '编辑消息模板' : '消息模板详情'
    this.templateId && this.getInfo()
    console.log(this.type)
  },
  methods: {
    getInfo() {
      requesInfoTemplateDetail({
        id: this.templateId,
      }).then((res) => {
        if (res.success) {
          this.form = res.data
        } else {
          this.$message({
            type: 'error',
            message: res.message,
          })
        }
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          const api = {
            add: requestAddInfoTemplate,
            edit: requestEditInfoTemplate,
          }
          api[this.type](this.form).then((res) => {
            if (res.success) {
              this.$message({
                type: 'success',
                message: res.message,
              })
              this.form = {}
              this.loading = false
              this.$emit('back')
            } else {
              this.loading = false
              this.$message({
                type: 'error',
                message: res.message,
              })
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
::v-deep .el-page-header {
  background-color: #ffff;
  padding: 20px;
  border-bottom: 1px solid #f0f0f0;
}
::v-deep .el-page-header__content {
  font-size: 16px;
  font-family: PingFangSC, PingFang SC;
  font-weight: 500;
  color: #262626;
}
.bg-white {
  min-height: 100%;
}
.header-wrap {
  padding-bottom: 16px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.15);
  margin-bottom: 16px;
}

.bold {
  font-weight: bolder;
  margin-bottom: 20px;
}
</style>
